/* This doesn't work, but might be worth saving for reference */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <linux/dvb/dmx.h>

int main(int argc, char **argv)
{
    int adapter = 0;
    int dmx = 0;
    int n;
    FILE *usage = NULL;
    
    for (n = 0; n < argc; ++n)
    {
        if (!strcmp(argv[n], "-h") || !strcmp(argv[n], "--help"))
        {
            usage = stdout;
            break;
        }
        else if (!strcmp(argv[n], "-a"))
        {
            if (n < argc - 1)
            {
                adapter = atoi(argv[++n]);
            }
            else
            {
                usage = stderr;
                break;
            }
        }
        else if (!strcmp(argv[n], "-f"))
        {
            if (n < argc - 1)
            {
                dmx = atoi(argv[++n]);
            }
            else
            {
                usage = stderr;
                break;
            }
        }
    }
    if (usage)
    {
        fprintf(usage, "Usage: dmxcaps [-a ADAPTER] [-d DMX]\n");
        exit(usage == stderr);
    }
    
    char devname[64];
    sprintf(devname, "/dev/dvb/adapter%d/demux%d", adapter, dmx);
    int fd = open(devname, O_RDWR | O_NONBLOCK);
    if (fd < 0)
    {
        perror("Error opening device");
        exit(1);
    }
    dmx_caps_t caps;
    if (ioctl(fd, DMX_GET_CAPS, &caps) < 0)
    {
        perror("Error reading caps with ioctl");
        exit(1);
    }
    printf("Caps: 0x%x\n", caps.caps);
    printf("Number of decoders: %d\n", caps.num_decoders);
    close(fd);
    return 0;
}
